package indi.jp.project.business.device.controller;

import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SimpleEntityUtil;
import indi.jp.project.business.device.entity.Device;
import indi.jp.project.business.device.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Map;

@RestController
@RequestMapping("/yunying/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/queryGrid")
    @ResponseBody
    public JsonResult queryGrid(@ModelAttribute Device entity, HttpServletRequest request) throws Exception {
        SimpleEntityUtil<Device> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        return deviceService.queryGrid(entity);
    }

    @PostMapping("/save")
    @ResponseBody
    public JsonResult save(@RequestBody Device entity, HttpServletRequest request) throws Exception {
        SimpleEntityUtil<Device> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        return deviceService.save(entity);
    }

    @GetMapping("/findById")
    @ResponseBody
    public JsonResult findById(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        return deviceService.findById(id);
    }

    @GetMapping("/queryHistoryGrid")
    @ResponseBody
    public JsonResult queryHistoryGrid(HttpServletRequest request) throws Exception {
        String deviceId = request.getParameter("deviceId");
        return deviceService.queryHistoryGrid(deviceId);
    }

    @PostMapping("/deleteById")
    public JsonResult deleteById(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        return deviceService.deleteById(id);
    }
}
